package com.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.Admin;
import com.project.entity.po.Comment;
import com.project.entity.po.Member;
import com.project.entity.po.User;
import com.project.entity.qo.CommentQo;
import com.project.exception.ServiceException;
import com.project.mapper.AdminMapper;
import com.project.mapper.CommentMapper;
import com.project.mapper.MemberMapper;
import com.project.mapper.UserMapper;
import com.project.entity.qo.MemberQo;
import com.project.entity.qo.UserQo;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.AdminService;
import com.project.utils.ShiroUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName:
 * @Package:
 * @ClassName:
 * @Author: ZhaoLongShi
 * @CreateDate:
 */
@SuppressWarnings("all")
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private CommentMapper commentMapper;

    @Override
    public ResponseResult selectAdminByName(String username) {
        //查询用户名是否存在
        Admin admin = adminMapper.selectAdminByName(username);
        Session session = ShiroUtils.getSession();
        session.setAttribute("admin", admin);
        return ResponseResult.success(ResultCode.SUCCESS, admin);
    }

    /**
     * 查找用户（分页）
     *
     * @param current
     * @return
     */
    @Override
    public ResponseResult<Page<User>> selectAllUser(int current) {
        Page<User> users = userMapper.selectAllUser(current);
        if (ObjectUtils.isEmpty(users)) {
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(users);
    }

    /**
     * 删除用户（批量）
     *
     * @param idList
     * @return
     */
    @Override
    public ResponseResult<ResultCode> deleteUserByIds(List<Long> idList) {
        if (idList == null) {
            throw new ServiceException(ResultCode.PARAMS_NOT_IS_BLANK);
        }
        int count = userMapper.deleteUserByIds(idList);
        if (count == 0) {
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 添加用户
     *
     * @param userQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addUser(UserQo userQo) {
        String sha256 = ShiroUtils.sha256(userQo.getPassword(), null);
        userQo.setPassword(sha256);
        User user = new User();
        BeanUtils.copyProperties(userQo, user);
        User user1 = userMapper.checkNameAndPhone(user.getUsername(), null);
        if (!ObjectUtils.isEmpty(user1)) {
            throw new ServiceException(ResultCode.ACCOUNT_IS_EXISTENT);
        }

        int count = userMapper.insert(user);
        if (count == 0) {
            throw new ServiceException(ResultCode.INSERT_FAILED);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 更新用户
     *
     * @param userQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateUserById(UserQo userQo) {
        User user = new User();
        BeanUtils.copyProperties(userQo, user);
        user.setPassword(ShiroUtils.sha256(user.getPassword(), null));
        int count = userMapper.updateById(user);
        if (count == 0) {
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }
}
